package com.sdy.healthcode.biz.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.model.ApplyPost;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author zzq
 * @since 2020-02-18
 */
@Data
@Accessors(chain = true)
@ApiModel(value = "用户申请表单数据")
public class UserApplyPost {

    @ApiModelProperty(value = "需要健康码的用户ID，为自己申请可置为null", hidden=true)
    private Integer userId;

    @ApiModelProperty(value = "姓名", required = true)
    private String name;

    @ApiModelProperty(value = "身份 1-就诊患者 2-陪同家属 3-本院人员", required = true, allowableValues = "1,2,3")
    private Integer type;

    @ApiModelProperty(value = "预约时间", hidden = true)
    @JsonFormat(pattern = DateUtil.DATE_FORMAT)
    private Date appointmentTime;

    @ApiModelProperty(value = "手机号", required = true)
    private String phone;

    @ApiModelProperty(value = "证件号", required = true)
    private String cardId;

    @ApiModelProperty(value = "申请人ID", hidden = true)
    private Integer applyUserId;

    @ApiModelProperty(value = "证件类型 1-身份证 2-护照 3-台胞证 4-回乡证 5-军官证", required = true, allowableValues = "1,2,3,4,5")
    private Integer cardType;

    @ApiModelProperty(value = "当前所在地址")
    private Address tripAddr;

    @ApiModelProperty(value = "是否接触武汉人员", required = true, allowableValues = "是,否")
    private String healthTouchWuhan;

    @ApiModelProperty(value = "是否接触确诊病人", required = true, allowableValues = "是,否")
    private String healthTouchPatient;

    @ApiModelProperty(value = "是否去过危险地区", required = true, allowableValues = "是,否")
    private String healthTouchAddrDanger;

    @ApiModelProperty(value = "健康状况", allowableValues = "无异常,发热（体温≥37.3℃）,乏力,干咳,鼻塞,流涕,咽痛,腹泻")
    private List<String> healthAbnormalDetail;

    @ApiModelProperty(value = "家属")
    private List<FamilyMember> familyMember;

    @ApiModelProperty(value = "身份证图片")
    private List<String> idCardImg;;

    @ApiModelProperty(value = "专家预约")
    @Deprecated
    private ExpertApplyDTO expertApply;
    
    public static UserApplyPost from(ApplyPost applyPost) {
        if (applyPost == null) {
            return null;
        }
        UserApplyPost uap = new UserApplyPost();
        uap.setName(applyPost.getName())
                .setType(applyPost.getType())
                .setAppointmentTime(applyPost.getAppointmentTime())
                .setPhone(applyPost.getPhone())
                .setCardId(applyPost.getCardId())
                .setCardType(applyPost.getCardType())
                .setTripAddr(Address.fromCodeStrToSingleAddr(applyPost.getTripAddrCode()))
                .setHealthTouchWuhan(applyPost.getHealthTouchWuhan())
                .setHealthTouchPatient(applyPost.getHealthTouchPatient())
                .setHealthTouchAddrDanger(applyPost.getHealthTouchAddrDanger())
                .setHealthAbnormalDetail(StringUtil.isBlank(applyPost.getHealthAbnormalDetail())
                        ? Collections.emptyList()
                        : Arrays.asList(applyPost.getHealthAbnormalDetail().split("##")))
                .setFamilyMember(FamilyMember.fromStr(applyPost.getFamilyMember()))
                .setIdCardImg(StringUtil.isBlank(applyPost.getIdCardImg())
                        ? Collections.emptyList()
                        : Arrays.asList(applyPost.getIdCardImg().split(";")));
        return uap;
    }
}
